/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.address.URI;
import javax.sip.header.ErrorInfoHeader;
import java.text.ParseException;

/**
 * The Error-Info header field provides a pointer to additional information
 * about the error status response.
 * SIP UACs have user interface capabilities ranging from pop-up windows and
 * audio on PC softclients to audio-only on "black" phones or endpoints
 * connected via gateways.  Rather than forcing a server generating an error
 * to choose between sending an error status code with a detailed reason
 * phrase and playing an audio recording, the Error-Info header field allows
 * both to be sent. The UAC then has the choice of which error indicator to
 * render to the caller.
 * <p/>
 * A UAC MAY treat a SIP or SIPS URI in an Error-Info header field as if it
 * were a Contact in a redirect and generate a new INVITE, resulting in a
 * recorded announcement session being established.  A non-SIP URI MAY be
 * rendered to the user.
 * <p/>
 * Examples:<br>
 * <code>SIP/2.0 404 The number you have dialed is not in service<br>
 * Error-Info: sip:not-in-service-recording@atlanta.com</code>
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ErrorInfoHeaderImpl extends ParametrizedHeaderBase implements ErrorInfoHeader {
    private URI errorInfo;
    private String errorMessage;

    private static final ErrorInfoHeaderParser parser = new ErrorInfoHeaderParser();

    public ErrorInfoHeaderImpl() {
        super(ErrorInfoHeader.NAME);
    }

    ErrorInfoHeaderImpl(byte[] data, int start, int end) {
        super(ErrorInfoHeader.NAME, data, start, end);
    }

    public Object clone() {
        final ErrorInfoHeaderImpl header = new ErrorInfoHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.cloneParameters(this);
            if (this.errorInfo != null) header.errorInfo = (URI) this.errorInfo.clone();
            header.errorMessage = this.errorMessage;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return '<' + errorInfo.toString() + '>';
    }

    /**
     * Sets the ErrorInfo of the ErrorInfoHeader to the <var>errorInfo</var>
     * parameter value.
     *
     * @param errorInfo the new ErrorInfo of this ErrorInfoHeader.
     */

    public void setErrorInfo(URI errorInfo) {
        parse();
        this.errorInfo = errorInfo;
        invalidateHeaderData();
    }

    /**
     * Returns the ErrorInfo value of this ErrorInfoHeader. This message
     * may return null if a String message identifies the ErrorInfo.
     *
     * @return the URI representing the ErrorInfo.
     */

    public URI getErrorInfo() {
        parse();
        return this.errorInfo;
    }

    /**
     * Sets the Error information message to the new <var>message</var> value
     * supplied to this method.
     *
     * @param message the new string value that represents the error message.
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the error message.
     */

    public void setErrorMessage(String message) throws ParseException {
        parse();
        this.errorMessage = message;
        invalidateHeaderData();
    }

    /**
     * Get the Error information message of this ErrorInfoHeader.
     *
     * @return the stringified version of the ErrorInfo header.
     */

    public String getErrorMessage() {
        parse();
        return this.errorMessage;
    }
}
/*
 * $Log$
 */